package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import com.wpen.unit.infra.repository.job.DependonRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.AppVariables;
import com.wpen.unit.enums.DependType;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.DependonMapper;
import com.wpen.unit.infra.mybatis.job.po.DependonPo;
import com.wpen.unit.infra.mybatis.job.po.DependonPo.DependonColumns;
import com.wpen.unit.utils.StringUtil;

/**
 * 依赖信息维护.
 * 
 * @author pg
 *
 */
@Repository
@Transactional
public class DependonRepositoryImpl extends ServiceImpl<DependonMapper, DependonPo> implements DependonRepository {

	@Override
	public List<DependonPo> queryAllBatchs(String bizCode) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		if (StringUtil.isNotEmpty(bizCode)) {
			queryWrapper.eq(DependonColumns.BIZ_CODE.name(), bizCode);
		}
		queryWrapper.eq(DependonColumns.SELF_TYPE.name(), DependType.Batch.value());
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public List<DependonPo> queryAllSubByParent(String dpnBizCode, String dpnBatchCode, String dpnTaskCode,
			boolean includeChild) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(DependonColumns.SELF_TYPE.name(), DependType.Task.value());
		queryWrapper.eq(DependonColumns.DPDON_BIZ_CODE.name(), dpnBizCode);
		queryWrapper.eq(DependonColumns.DPDON_BATCH_CODE.name(), dpnBatchCode);
		if (StringUtil.isNotEmpty(dpnTaskCode)) {
			queryWrapper.eq(DependonColumns.DPDON_TASK_CODE.name(), dpnTaskCode);
			if (!includeChild) {
				queryWrapper.eq(DependonColumns.JOB_CODE.name(), AppVariables.DEPENDON_PHD);
			}
		} else {
			if (!includeChild) {
				queryWrapper.eq(DependonColumns.JOB_CODE.name(), AppVariables.DEPENDON_PHD);
			}
		}
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public List<DependonPo> queryAllPrtByOwn(String bizCode, String batchCode, String taskCode, String jobCode,
			Boolean includeParent, Boolean includeSub) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(DependonColumns.BIZ_CODE.name(), bizCode);
		queryWrapper.eq(DependonColumns.BATCH_CODE.name(), batchCode);
		if (StringUtil.isNotEmpty(taskCode)) {
			queryWrapper.eq(DependonColumns.TASK_CODE.name(), taskCode == null ? AppVariables.DEPENDON_PHD : taskCode);
		}
		queryWrapper.eq(DependonColumns.JOB_CODE.name(), jobCode == null ? AppVariables.DEPENDON_PHD : jobCode);
		if (includeParent != null && !includeParent) {
			queryWrapper.ne(DependonColumns.DPDON_TASK_CODE.name(), AppVariables.DEPENDON_PHD);
		}
		if (includeSub != null && !includeSub) {
			queryWrapper.eq(DependonColumns.JOB_CODE.name(), AppVariables.DEPENDON_PHD);
		}
		return getBaseMapper().selectList(queryWrapper);
	}

	public List<DependonPo> queryByOwn(String bizCode, String batchCode, String taskCode, String jobCode,
			DependType dependType) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(DependonColumns.BIZ_CODE.name(), bizCode);
		queryWrapper.eq(DependonColumns.BATCH_CODE.name(), batchCode);
		queryWrapper.eq(DependonColumns.TASK_CODE.name(), taskCode == null ? AppVariables.DEPENDON_PHD : taskCode);
		queryWrapper.eq(DependonColumns.JOB_CODE.name(), jobCode == null ? AppVariables.DEPENDON_PHD : jobCode);
		queryWrapper.eq(DependonColumns.SELF_TYPE.name(), dependType.value());
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public List<DependonPo> queryByParent(String dpnBizCode, String dpnBatchCode, String dpnTaskCode, String dpnJobCode,
			DependType dependType) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(DependonColumns.DPDON_BIZ_CODE.name(), dpnBizCode);
		queryWrapper.eq(DependonColumns.DPDON_BATCH_CODE.name(), dpnBatchCode);
		queryWrapper.eq(DependonColumns.DPDON_TASK_CODE.name(),
				dpnTaskCode == null ? AppVariables.DEPENDON_PHD : dpnTaskCode);
		queryWrapper.eq(DependonColumns.DPDON_JOB_CODE.name(),
				dpnJobCode == null ? AppVariables.DEPENDON_PHD : dpnJobCode);
		queryWrapper.eq(DependonColumns.SELF_TYPE.name(), dependType.value());
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public boolean deletePrtByCode(String bizCode, String batchCode, String taskCode) {
		UpdateWrapper<DependonPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(DependonColumns.BIZ_CODE.name(), bizCode);
		updateWrapper.eq(DependonColumns.BATCH_CODE.name(), batchCode);
		updateWrapper.eq(DependonColumns.TASK_CODE.name(), taskCode);
		return super.remove(updateWrapper);
	}

	@Override
	public boolean deleteSubByCode(String bizCode, String batchCode, String taskCode) {
		UpdateWrapper<DependonPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(DependonColumns.DPDON_BIZ_CODE.name(), bizCode);
		updateWrapper.eq(DependonColumns.DPDON_BATCH_CODE.name(), batchCode);
		updateWrapper.eq(DependonColumns.DPDON_TASK_CODE.name(), taskCode);
		return super.remove(updateWrapper);
	}

	@Override
	public long count(DependonPo dependonPo) {
		QueryWrapper<DependonPo> queryWrapper = createQuery(dependonPo);
		return super.count(queryWrapper);
	}

	@Override
	public List<DependonPo> queryList(int index, int size, DependonPo dependonPo) {
		QueryWrapper<DependonPo> queryWrapper = createQuery(dependonPo);
		queryWrapper.orderByAsc(DependonColumns.BIZ_CODE.name());
		queryWrapper.orderByAsc(DependonColumns.BATCH_CODE.name());
		if (size > 0) {
			Page<DependonPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		// queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
	}

	private QueryWrapper<DependonPo> createQuery(DependonPo dependonPo) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		if (dependonPo.getId() != null) {
			queryWrapper.eq(DependonColumns.ID.name(), dependonPo.getId());
		}
		if (StringUtil.isNotEmpty(dependonPo.getSelfType())) {
			queryWrapper.eq(DependonColumns.SELF_TYPE.name(), dependonPo.getSelfType());
		}
		if (StringUtil.isNotEmpty(dependonPo.getBizCode())) {
			queryWrapper.eq(DependonColumns.BIZ_CODE.name(), dependonPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getBatchCode())) {
			queryWrapper.eq(DependonColumns.BATCH_CODE.name(), dependonPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getTaskCode())) {
			queryWrapper.eq(DependonColumns.TASK_CODE.name(), dependonPo.getTaskCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getJobCode())) {
			queryWrapper.eq(DependonColumns.JOB_CODE.name(), dependonPo.getJobCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonBizCode())) {
			queryWrapper.eq(DependonColumns.DPDON_BIZ_CODE.name(), dependonPo.getDpdonBizCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonBatchCode())) {
			queryWrapper.eq(DependonColumns.DPDON_BATCH_CODE.name(), dependonPo.getDpdonBatchCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonTaskCode())) {
			queryWrapper.eq(DependonColumns.DPDON_TASK_CODE.name(), dependonPo.getDpdonTaskCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonJobCode())) {
			queryWrapper.eq(DependonColumns.DPDON_JOB_CODE.name(), dependonPo.getDpdonJobCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getEnabled())) {
			queryWrapper.eq(DependonColumns.ENABLED.name(), dependonPo.getEnabled());
		}
		return queryWrapper;
	}

	@Override
	public DependonPo queryFirst(DependonPo dependonPo) {
		QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
		if (dependonPo.getId() != null) {
			queryWrapper.eq(DependonColumns.ID.name(), dependonPo.getId());
		}
		if (StringUtil.isNotEmpty(dependonPo.getSelfType())) {
			queryWrapper.eq(DependonColumns.SELF_TYPE.name(), dependonPo.getSelfType());
		}
		if (StringUtil.isNotEmpty(dependonPo.getBizCode())) {
			queryWrapper.eq(DependonColumns.BIZ_CODE.name(), dependonPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getBatchCode())) {
			queryWrapper.eq(DependonColumns.BATCH_CODE.name(), dependonPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getTaskCode())) {
			queryWrapper.eq(DependonColumns.TASK_CODE.name(), dependonPo.getTaskCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getJobCode())) {
			queryWrapper.eq(DependonColumns.JOB_CODE.name(), dependonPo.getJobCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonBizCode())) {
			queryWrapper.eq(DependonColumns.DPDON_BIZ_CODE.name(), dependonPo.getDpdonBizCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonBatchCode())) {
			queryWrapper.eq(DependonColumns.DPDON_BATCH_CODE.name(), dependonPo.getDpdonBatchCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonTaskCode())) {
			queryWrapper.eq(DependonColumns.DPDON_TASK_CODE.name(), dependonPo.getDpdonTaskCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getDpdonJobCode())) {
			queryWrapper.eq(DependonColumns.DPDON_JOB_CODE.name(), dependonPo.getDpdonJobCode());
		}
		if (StringUtil.isNotEmpty(dependonPo.getEnabled())) {
			queryWrapper.eq(DependonColumns.ENABLED.name(), dependonPo.getEnabled());
		}
		// queryWrapper.orderBy(true, false, DependonPoColumns.ID.name());
		return super.getOne(queryWrapper);
	}

	@Override
	public DependonPo queryById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public boolean insert(DependonPo dependonPo) {
		// 替换不建议的null为固定字符串
		changeNull(dependonPo);

		dependonPo.setVersion(1);
		dependonPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
		return super.save(dependonPo);
	}

	private void changeNull(DependonPo dependonPo) {
		if (StringUtil.isEmpty(dependonPo.getTaskCode())) {
			dependonPo.setTaskCode(AppVariables.DEPENDON_PHD);
		}
		if (StringUtil.isEmpty(dependonPo.getJobCode())) {
			dependonPo.setJobCode(AppVariables.DEPENDON_PHD);
		}
		if (StringUtil.isEmpty(dependonPo.getDpdonTaskCode())) {
			dependonPo.setDpdonTaskCode(AppVariables.DEPENDON_PHD);
		}
		if (StringUtil.isEmpty(dependonPo.getDpdonJobCode())) {
			dependonPo.setDpdonJobCode(AppVariables.DEPENDON_PHD);
		}
	}

	@Override
	public boolean insertBatch(List<DependonPo> listDependonPos) {
		// 替换不建议的null为固定字符串
		listDependonPos.forEach(act -> {
			changeNull(act);
		});

		int rs = getBaseMapper().insertBatchSomeColumn(listDependonPos);
		return SqlHelper.retBool(rs);
	}

	@Override
	public boolean updateById(DependonPo dependonPo) {
		UpdateWrapper<DependonPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(DependonColumns.ID.name(), dependonPo.getId());
		updateWrapper.eq(DependonColumns.VERSION.name(), dependonPo.getVersion());
		dependonPo.setVersion(dependonPo.getVersion() + 1);
		boolean rs = super.update(dependonPo, updateWrapper);
		// 根据ID更新所有字段，空值也更新
		// int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
		// boolean rs = SqlHelper.retBool(num);
		if (!rs) {
			throw new UpdateZeroException();
		}
		return rs;
	}

	@Override
	public boolean deleteById(Serializable id) {
		UpdateWrapper<DependonPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(DependonColumns.ID.name(), id);
		return super.remove(updateWrapper);
	}

	public boolean deleteByIds(Serializable[] ids) {
		UpdateWrapper<DependonPo> updateWrapper = Wrappers.update();
		updateWrapper.in(DependonColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
	}

//    public DependonPo queryByExample(DependonPo dependonPo) {
//        QueryWrapper<DependonPo> queryWrapper = Wrappers.query();
//        if (dependonPo.getId() != null) {
//            queryWrapper.eq(DependonColumns.ID.name(), dependonPo.getId());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getSelfType())) {
//            queryWrapper.eq(DependonColumns.SELF_TYPE.name(), dependonPo.getSelfType());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getBizCode())) {
//            queryWrapper.eq(DependonColumns.BIZ_CODE.name(), dependonPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getBatchCode())) {
//            queryWrapper.eq(DependonColumns.BATCH_CODE.name(), dependonPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getTaskCode())) {
//            queryWrapper.eq(DependonColumns.TASK_CODE.name(), dependonPo.getTaskCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getJobCode())) {
//            queryWrapper.eq(DependonColumns.JOB_CODE.name(), dependonPo.getJobCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getDpdonBizCode())) {
//            queryWrapper.eq(DependonColumns.DPDON_BIZ_CODE.name(), dependonPo.getDpdonBizCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getDpdonBatchCode())) {
//            queryWrapper.eq(DependonColumns.DPDON_BATCH_CODE.name(), dependonPo.getDpdonBatchCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getDpdonTaskCode())) {
//            queryWrapper.eq(DependonColumns.DPDON_TASK_CODE.name(), dependonPo.getDpdonTaskCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getDpdonJobCode())) {
//            queryWrapper.eq(DependonColumns.DPDON_JOB_CODE.name(), dependonPo.getDpdonJobCode());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getEnabled())) {
//            queryWrapper.eq(DependonColumns.ENABLED.name(), dependonPo.getEnabled());
//        }
//        if (StringUtil.isNotEmpty(dependonPo.getCreateTime())) {
//            queryWrapper.eq(DependonColumns.CREATE_TIME.name(), dependonPo.getCreateTime());
//        }
//        if (dependonPo.getVersion() != null) {
//            queryWrapper.eq(DependonColumns.VERSION.name(), dependonPo.getVersion());
//        }
//		Page<DependonPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, DependonPoColumns.ID.name());
//    }

}
